<?php

class Admin_Model_Role extends Zend_Db_Table_Abstract
{
    protected $_name = 'role';
    
    public function getAllData(){
		$query = $this->select()
                        ->setIntegrityCheck(false)
                        ->from(array('r'=>$this->_name),array('r.roleId','r.roleName','r.resourceId'));
        return $this->fetchAll($query);
    }
    public function getData($data){
        $query = $this->select()
                        ->setIntegrityCheck(false)
                        ->from(array('r'=>$this->_name), array('r.roleId', 'r.roleName','r.resourceId'))
                        ->join(array('ur'=>'user_role'),'r.roleId = ur.roleId', array('ur.userId'));
        if(isset($data['adminId']) && $data['adminId']){
            $query->where('ur.userId = ?',$data['adminId']);
        }
       
        if(isset($data['resourceId']) && $data['resourceId']){
            $data['resourceId'] = addslashes($data['resourceId']);
            $query->where('resourceId like (\'%"'.$data['resourceId'].'"%\')');
        }
        return $this->fetchAll($query);
    }
    public function addData($roleName, $resourceId = ''){
        $data = array('roleName' => $roleName, 'resourceId' => $resourceId);
        $idAdd = $this->insert($data);
        return $idAdd;
    }
    public function deleteData($roleId){
        $this->delete('roleId = '.(int)$roleId);
    }
    public function updateData($roleId, $roleName,$resourceId = ''){
        $data = array('roleName' => $roleName, 'resourceId' => $resourceId);
        return $this->update($data, 'roleId = '.(int) $roleId);
    }
    public function getDataByRoleName($roleName){
        $query = $this->select()
                        ->from(array('r'=>$this->_name), array('r.roleId', 'r.roleName','r.resourceId'))
                        ->where('r.roleName = ?',$roleName);
                        
    	return $this->fetchRow($query);
    }
    public function getDataByRoleId($roleid){  
        $query = $this->select()
                        ->from(array('r'=>$this->_name), array('r.roleId', 'r.roleName','r.resourceId'))
                        ->where('r.roleId = ?',(int)$roleid);
                                        
    	return $this->fetchRow($query);
    }
    public function getDataByUserId($userId){
        $query = $this->select()
                        ->setIntegrityCheck(false)
                        ->from(array('r'=>$this->_name), array('r.roleId', 'r.roleName','r.resourceId'))
                        ->join(array('ur'=>'user_role'),'r.roleId = ur.roleId', array('ur.userId'))
                        ->where('ur.userId = ?',(int)$userId);
                                        
    	return $this->fetchAll($query);
    }
   
}

























